/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package components;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import nemeanshop.NemeanShop;
import nemeanshop.Product;

/**ˆ
 *
 * @author havardaxelsen
 */
public class NPopup extends JPopupMenu{
    
    private PopupPanel p;
    private JMenuItem menu = new JMenuItem();
    
    
    public NPopup(int rov, NemeanShop ns){
        
        menu.setFocusable(false);
        this.setFocusable(false);
        this.setBackground(new Color(213,213,213));
        p = new PopupPanel(rov, ns);
        this.setBackground(null);
        this.setOpaque(true);
        this.add(p);

    }
    
    public NPopup(int rov, NemeanShop ns, Product prod){
        this.setBackground(new Color(213,213,213));
       p = new PopupPanel(rov, ns, prod);
       this.setBackground(null);
       this.setOpaque(true);
        this.add(p);

    }
    
    @Override
    public int getWidth()
    {
        return this.getSize().width;
    }
    
    
}

class PopupPanel extends NPanel{
    
    private ImageIcon delimg       = new ImageIcon("src/GUInemeanshop/img/small/delete_24.png");
    private ImageIcon chaimg       = new ImageIcon("src/GUInemeanshop/img/small/brush_24.png");
    private ImageIcon delimgmo     = new ImageIcon("src/GUInemeanshop/img/small/delete_24_mo.png");
    private ImageIcon chaimgmo     = new ImageIcon("src/GUInemeanshop/img/small/brush_24_mo.png");
    private int rovv;
    private JButton change  = new JButton(chaimg);
    private JButton del  = new JButton(delimg);
    private NemeanShop ns;
    private Product prod;
    
    public PopupPanel(int rov, NemeanShop ns){
        this.setBorder(null);
        this.setBackground(new Color(213,213,213, 150));
        change.setRolloverIcon(chaimgmo);
        del.setRolloverIcon(delimgmo);
        change.setBackground(null);
        change.setBorder(null);
        del.setBackground(null);
        del.setBorder(null);
        del.setBorder(BorderFactory.createEmptyBorder(5,20,5,20));
        change.setBorder(BorderFactory.createEmptyBorder(5,20,5,20));
        
        this.rovv = rov;
        this.ns = ns;
        this.setLayout(new GridLayout(1,2));
        change.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae)
            {
                String input = JOptionPane.showInputDialog("Skriv inn antall varer du vil legge til!");
                int quantity = Integer.parseInt(input);
            }
        });
        del.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                JOptionPane.showMessageDialog(null, "Heisann!  Row: " + rovv);
                
            }
        });
        change.setFocusable(false);
        del.setFocusable(false);
        add(change);
        add(del);
        this.setFocusable(false);
    }
    
    public PopupPanel(int rov, final NemeanShop ns, final Product prod){
        this.setBorder(null);
        this.setBackground(new Color(213,213,213, 150));
        change.setRolloverIcon(chaimgmo);
        del.setRolloverIcon(delimgmo);
        change.setBackground(null);
        change.setBorder(null);
        del.setBackground(null);
        del.setBorder(null);
        del.setBorder(BorderFactory.createEmptyBorder(5,20,5,20));
        change.setBorder(BorderFactory.createEmptyBorder(5,20,5,20));
        
        this.rovv = rov;
        this.ns = ns;
        this.prod = prod;
        this.setLayout(new GridLayout(1,2));
        change.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae)
            {
                String input = JOptionPane.showInputDialog("Skriv inn antall varer du vil legge til / fjerne:");
                if (input != null)
                {
                    int quantity = Integer.parseInt(input);
                    ns.updateProductQuantity(prod.getBarcode(), quantity);
                }
            }
        });
        del.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                int val = JOptionPane.showConfirmDialog(null, "Er du sikker på at du vil slette denne varen?");
                if (val == 0)
                {
                    ns.deleteProduct(prod.getBarcode());
                }                
            }
        });
        change.setFocusable(false);
        del.setFocusable(false);
        add(change);
        add(del);
        this.setFocusable(false);
    }
    
}
